<?php declare(strict_types=1);


namespace App\Service;


use App\Dao\StudentDao;
use App\Model\Student;
use Hyperf\Di\Annotation\Inject;
use Swoole\Exception;

class StudentService
{
    /**
     * @Inject()
     * @var StudentDao
     */
    private $dao;



    /**
     * 通过学号查询
     * @param $stuNum
     * @return Student|\Hyperf\Database\Model\Builder|\Hyperf\Database\Model\Model|object
     * @throws Exception
     */
    public function findCheck($stuNum)
    {
        $model = $this->dao->findByNum($stuNum);
        if(!$model){
            throw new Exception('学号或密码错误');
        }
        return $model;
    }

    /**
     * 通过学号查询数据
     * @param $stuNum
     * @return \Hyperf\Database\Model\Builder|\Hyperf\Database\Model\Model|object|null
     */
    public function find($stuNum)
    {
        return  $this->dao->findByNum($stuNum);
    }

    /**
     * 修改密码
     */
    public function editPassword(Student $student, $pwd)
    {
        return $this->dao->editPassword($student, $pwd);
    }

    /**
     * 添加
     * @throws \Exception
     */
    public function add($params)
    {

        $one = $this->dao->findByNum($params['stu_num']);
        if($one) {
            throw new \Exception('学号已存在');
        }
        return $this->dao->addStu($params);
    }



}